package 二叉树中的最大路径和124;

import java.util.Arrays;

/**
 * @author 谢阳
 * @version 1.8.0_131
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    int max = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }

    public int dfs(TreeNode node) {
        if (node == null) {
            return 0;
        }

        int left = dfs(node.left);
        int right = dfs(node.right);

        left = Math.max(left, 0);//小于零则略过
        right = Math.max(right, 0);

        max = Math.max(max, node.val + left + right);//求每个局部最大路径
        return Math.max(left + node.val, right + node.val);//求单线最大路径

    }
}
